﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CarryOnBagManager : MonoBehaviour
{
    public MainItem mainItem;
    public SMainItem sMainItem;
    public GameObject parentObject;//背包格子
    public ObjectMessage objectTogglePrefab;//预制体
    Text moneyValue;//元宝数
    public Image[] btn;//四个按钮的图片
    public GameObject bombBox;//弹框
    public static CarryOnBagManager COBM;
    public int clickIndex = 0;//判断当前处于哪个界面，0全部，1装备，2技能，3材料
    public int idd;//点击弹框的装备id
    private void Awake()
    {
        COBM = this;
        moneyValue = GameObject.Find("moneyValue").GetComponent<Text>();
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
        //Item item = new Item
        //{
        //    id = mainItem.itemList.Count,
        //    itemCategory = 0,
        //    selled = false,
        //    itemName = "头盔",
        //    itemImage = mainItem.itemList[0].itemImage,
        //    itemGrade = 2,
        //    attribute = new float[] { 0, 0, 0, 0, 0, 0 }
        //};
        //mainItem.itemList.Add(item);
        //mainItem.CreateExampleAsset(mainItem.itemList.Count, item);
        
        DisplayAll();
    }
    
    void Update()
    {
        moneyValue.text = VariableDocument.GetInstance.money.ToString();
    }

    //全部
    public void DisplayAll()
    {
        clickIndex = 0;
        btn[0].sprite = Resources.Load<Sprite>("CarryOnBag/liang");
        btn[1].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[2].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[3].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        //销毁背包格子下的子物体
        for (int i = 0; i < parentObject.transform.childCount; i++)
        {
            Destroy(parentObject.transform.GetChild(i).gameObject);
        }
        //遍历mainItem
        DisplayMainItem();
        //遍历sMainItem
        DisplaySMainItem();

    }

    //装备
    public void DisplayEquipMent()
    {
        clickIndex = 1;
        btn[0].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[1].sprite = Resources.Load<Sprite>("CarryOnBag/liang");
        btn[2].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[3].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        //销毁背包格子下的子物体
        for (int i = 0; i < parentObject.transform.childCount; i++)
        {
            Destroy(parentObject.transform.GetChild(i).gameObject);
        }
        DisplayMainItem();
    }
    void DisplayMainItem()
    {
        for (int a = 0; a < mainItem.itemList.Count; a++)
        {
            if (!mainItem.itemList[a].selled)
            {
                bool canNew = true;
                for (int b = 0; b < VariableDocument.GetInstance.equiped.Length; b++)
                {
                    if (VariableDocument.GetInstance.equiped[b] == a)
                    {
                        canNew = false;
                        break;
                    }
                }
                if (canNew)
                {
                    //加载预制体
                    ObjectMessage newObjectToggle = Instantiate(objectTogglePrefab);
                    //指定父物体
                    newObjectToggle.transform.SetParent(parentObject.transform);
                    //指定大小
                    newObjectToggle.transform.localScale = new Vector3(1f, 1f, 1f);
                    //单选
                    newObjectToggle.GetComponent<Toggle>().group = parentObject.GetComponent<ToggleGroup>();
                    //展示的装备的图片
                    newObjectToggle.image.sprite = mainItem.itemList[a].itemImage;
                    //装备的等级
                    newObjectToggle.text.text = "+" + mainItem.itemList[a].itemGrade;
                    //装备的id号
                    newObjectToggle.id = mainItem.itemList[a].id;
                    //类别
                    newObjectToggle.kinds = 0;
                }
            }
            
        }
    }
    //技能
    public void DisplaySkill()
    {
        clickIndex = 2;
        btn[0].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[1].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[2].sprite = Resources.Load<Sprite>("CarryOnBag/liang");
        btn[3].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        //销毁背包格子下的子物体
        for (int i = 0; i < parentObject.transform.childCount; i++)
        {
            Destroy(parentObject.transform.GetChild(i).gameObject);
        }
        DisplaySMainItem();
    }
    void DisplaySMainItem()
    {
        for (int a = 0; a < sMainItem.itemList.Count; a++)
        {
            if(sMainItem.itemList[a].fragmentsNum > 0)
            {
                //加载预制体
                ObjectMessage newObjectToggle = Instantiate(objectTogglePrefab);
                //指定父物体
                newObjectToggle.transform.SetParent(parentObject.transform);
                //指定大小
                newObjectToggle.transform.localScale = new Vector3(1f, 1f, 1f);
                //单选
                newObjectToggle.GetComponent<Toggle>().group = parentObject.GetComponent<ToggleGroup>();
                //展示的装备的图片
                newObjectToggle.image.sprite = sMainItem.itemList[a].sitemImage;
                //装备的等级
                newObjectToggle.text.text = "" + sMainItem.itemList[a].fragmentsNum;
                //装备的id号
                newObjectToggle.id = sMainItem.itemList[a].sid;
                //类别
                newObjectToggle.kinds = 1;
            }
        }
    }

    //点击材料按钮
    public void Display3()
    {
        clickIndex = 3;
        btn[0].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[1].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[2].sprite = Resources.Load<Sprite>("CarryOnBag/an");
        btn[3].sprite = Resources.Load<Sprite>("CarryOnBag/liang");
        //销毁背包格子下的子物体
        for (int i = 0; i < parentObject.transform.childCount; i++)
        {
            Destroy(parentObject.transform.GetChild(i).gameObject);
        }
    }
    //点击出售
    public void ClickSell()
    {
        VariableDocument.GetInstance.money += ((mainItem.itemList[idd].itemGrade+1) * 15);
        
        //对equiped[]进行更新
        for (int h = 0; h < 6; h++)
        {
            if (VariableDocument.GetInstance.equiped[h] == mainItem.itemList[idd].id)//只需要更改已经穿戴的装备的id
            {
                VariableDocument.GetInstance.equiped[h] -= 1;
            }

        }
        mainItem.itemList[idd].selled = true;
        CloseBombBox();
    }
    //点击穿戴
    public void ClickDress()
    {
        VariableDocument.GetInstance.equiped[mainItem.itemList[idd].itemCategory] = idd;
        CloseBombBox();
    }
    //点击空白处关闭
    public void CloseBombBox()
    {
        bombBox.SetActive(false);
        if(clickIndex == 0){
            DisplayAll();
        }
        if (clickIndex == 1)
        {
            DisplaySkill();
        }
    }
    
}
